/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.broker;

import java.util.ArrayList;
import sahd.broker.server.ServerMachine;

/**
 *
 * @author Romms
 */
public class RegisteredServersRepository {
    ArrayList <ServerMachine> registeredServers;

    public RegisteredServersRepository(){
        this.registeredServers=new ArrayList <ServerMachine>();
    }

    public boolean serverMachineExists(String serverIpAddress){
        for(int i=0;i<this.registeredServers.size();i++){
            if(this.registeredServers.get(i).getServerMchingeIpAddres().compareTo(serverIpAddress) ==0)
                return true;
        }
        return false;
    }

    public boolean serverMachineExists(ServerMachine serverMachine){
        for(int i=0;i<this.registeredServers.size();i++){
            if(this.registeredServers.get(i).getServerMchingeIpAddres().compareTo(serverMachine.getServerMchingeIpAddres()) ==0)
                return true;
        }
        return false;
    }

    private ServerMachine findServerMachine(ServerMachine serverMachine){
        for(int i=0;i<this.registeredServers.size();i++){
            if(this.registeredServers.get(i).getServerMchingeIpAddres().compareTo(serverMachine.getServerMchingeIpAddres()) ==0)
                return this.registeredServers.get(i);
        }
        return null;
    }

    public boolean registerServerMachine(ServerMachine serverMachine){
        if(serverMachineExists(serverMachine)){
            return false;
        }
        this.registeredServers.add(serverMachine);
        return true;
    }

    public boolean unregisterClientMachine(ServerMachine serverMachine){
        if(serverMachineExists(serverMachine)){
            this.registeredServers.remove(findServerMachine(serverMachine));
            return true;
        }
        return false;
    }

}
